

from pymysql import Connection
conn=None
try:

    conn=Connection(
        user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
        password="root",
        host="localhost",
        port=3306,
        charset="utf8",
        database='db2'
    )
except Exception as e:
    print(e)
finally:
    if conn:
        conn.close()
print(conn)
print(type(conn))